package jp.co.cos.ittrick.channel.controller;

import jp.co.cos.ittrick.channel.model.PreModel;
import jp.co.cos.ittrick.channel.service.PreService;
import jp.co.cos.ittrick.channel.util.Utility;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

/**
 * <pre>
 * Preダウンロードコントロール
 * </pre>
 * @author takahara
 *
 */
public class PreDownloadController extends Controller {

    private PreService service = new PreService();

    @Override
    public Navigation run() {

        String preKey = asString("pre");
        Utility utility = new Utility();

        if(preKey == null || preKey.equals("")) {
            return null;
        }

        PreModel preModel = service.getPreByKey(Integer.valueOf(preKey));

        if(preModel == null) {
            return null;
        }

        // バイトの取得
        byte[] preBytes = utility.htmlUnescape(preModel.getContentString()).getBytes();


        download(preModel.getName(), preBytes);

        return null;
    }
}
